package cn.wtg.kotlinlib.bindingAdapter

import android.util.Log
import androidx.databinding.BindingAdapter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * author: wtg
 * date:2020/3/16 0016
 * desc: 刷新和加载更多 adapter
 */
object RefreshBindingAdapter {
    /**
     * 自动刷新
     *
     * @param smartRefreshLayout 刷新控件
     * @param autoRefresh        是否自动刷新 true 是
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_autoRefresh"], requireAll = false)
    fun autoRefresh(smartRefreshLayout: SmartRefreshLayout, autoRefresh: Boolean) {
        if (autoRefresh) {
           val result = smartRefreshLayout.autoRefresh()
            Log.e("tag","$result")
        }
    }

    /**
     * 自动刷新
     *
     * @param smartRefreshLayout 刷新控件
     * @param autoLoadMore       是否自动刷新 true 是
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_autoLoadMore"], requireAll = false)
    fun autoLoadMore(smartRefreshLayout: SmartRefreshLayout, autoLoadMore: Boolean) {
        smartRefreshLayout.setEnableAutoLoadMore(autoLoadMore)
    }

    /**
     * 刷新控件是否再刷新
     *
     * @param smartRefreshLayout 刷新控件
     * @param isRefreshing       true 正在刷新 false刷新完毕
     */
    @JvmStatic
    @BindingAdapter("binding_finishRefresh")
    fun isRefresh(smartRefreshLayout: SmartRefreshLayout, isRefreshing: Boolean) {
        if (isRefreshing) {
            smartRefreshLayout.finishRefresh()
        }
    }

    /**
     * 刷新控件是否再加载更多
     *
     * @param smartRefreshLayout 刷新控件
     * @param isLoadingMore      true 正在刷新 false刷新完毕
     */
    @JvmStatic
    @BindingAdapter("binding_finishLoadMore")
    fun isLoadingMore(smartRefreshLayout: SmartRefreshLayout, isLoadingMore: Boolean) {
        if (!isLoadingMore) {
            smartRefreshLayout.finishLoadMore()
        }
    }

    /**
     * 是否刷新
     *
     * @param smartRefreshLayout 刷新控件
     * @param enableRefresh      true 可刷新 false未刷新
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_enableRefresh"], requireAll = false)
    fun enableRefresh(smartRefreshLayout: SmartRefreshLayout?, enableRefresh: Boolean) {
        smartRefreshLayout?.setEnableRefresh(enableRefresh)
    }

    /**
     * 是否加载
     *
     * @param smartRefreshLayout 刷新控件
     * @param enableLoadMore     true 可加载更多 false不可加载更多
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_enableLoadMore"], requireAll = false)
    fun enableLoadMore(smartRefreshLayout: SmartRefreshLayout?, enableLoadMore: Boolean) {
        smartRefreshLayout?.setEnableRefresh(enableLoadMore)
    }

    /**
     * 设置 下拉刷新，上拉加载的监听
     *
     * @param smartRefreshLayout 控件
     * @param onRefreshListener  刷新监听
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_onRefreshListener"], requireAll = false)
    fun setRefreshListener(
        smartRefreshLayout: SmartRefreshLayout,
        onRefreshListener: OnRefreshListener?,
    ) {
        smartRefreshLayout.setOnRefreshListener(onRefreshListener)
    }

    /**
     * 设置 下拉刷新，上拉加载的监听
     *
     * @param smartRefreshLayout 控件
     * @param onLoadMoreListener  加载更多监听
     */
    @JvmStatic
    @BindingAdapter(value = ["binding_onLoadMoreListener"], requireAll = false)
    fun setLoadMoreListener(
        smartRefreshLayout: SmartRefreshLayout,
        onLoadMoreListener: OnLoadMoreListener?,
    ) {
        smartRefreshLayout.setOnLoadMoreListener(onLoadMoreListener)
    }
}